package com.liyujie.networkrequest.ui.start;

import android.Manifest;
import android.os.Bundle;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.liyujie.networkrequest.R;
import com.liyujie.networkrequest.databinding.ActivityStartBinding;
import com.liyujie.networkrequest.ui.BaseActivity;
import com.liyujie.networkrequest.ui.home.HomeActivity;
import com.liyujie.networkrequest.utils.PermissionsUilst;

import java.util.List;

public class StartActivity extends BaseActivity<ActivityStartBinding> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_start;
    }


    @Override
    public void initView() {
        super.initView();

    }


    @Override
    public void initData() {
        super.initData();

    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        /**
         * 权限
         */
        if(PermissionsUilst.setAuthority(mContext)){
            ActivityUtils.startActivity(HomeActivity.class);
            ActivityUtils.finishActivity(StartActivity.class);
        }else {
            XXPermissions.with(mContext).permission(Permission.MANAGE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(List<String> permissions, boolean all) {
                            if (all) {
                                ActivityUtils.startActivity(HomeActivity.class);
                                ActivityUtils.finishActivity(StartActivity.class);
                            }else {
                                ToastUtils.showShort("部分权限未正常授予");
                            }
                        }
                        @Override
                        public void onDenied(List<String> permissions, boolean never) {
                            if (never) {
                                ToastUtils.showShort("被永久拒绝授权，请手动授予存储权限");
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                XXPermissions.startPermissionActivity(StartActivity.this, permissions);
                            } else {
                                ToastUtils.showShort("获取存储权限失败");
                            }
                        }
                    });
        }

    }
}
